//颜色引擎
//用法：给颜色加红，给颜色加蓝，给颜色加绿，生成以某个颜色为核心的随机颜色
var colorEngine = {
	AddRed : function(color, addvalue){
		var red = color.r + addvalue;

		var div = 0;
		if (red > 255){
			div = Math.round((255 - red) * 0.5);
			red = 255;
		}
		if (red < 0){
			div = Math.round((0 - red) * 0.5);
			red = 0;
		}

		if (div == 0)
			return cc.color(red,color.g,color.b,color.a);

		var green = color.g;
		var blue = color.b;
		green = green + div;
		green = green<0 ? 0 : green;
		green = green>255? 255 : green;
		blue = blue + div;
		blue = blue<0 ? 0 : blue;
		blue = blue>255? 255 : blue;
		return cc.color(red,green,blue,color.a);
	},
	
	AddGreen : function(color, addvalue){
		var green = color.g + addvalue;

		var div = 0;
		if (green > 255){
			div = Math.round((255 - green) * 0.5);
			green = 255;
		}
		if (green < 0){
			div = Math.round((0 - green) * 0.5);
			green = 0;
		}

		if (div == 0)
			return cc.color(color.r,green,color.b,color.a);

		var red = color.r;
		var blue = color.b;
		red = red + div;
		red = red<0 ? 0 : red;
		red = red>255? 255 : red;
		blue = blue + div;
		blue = blue<0 ? 0 : blue;
		blue = blue>255? 255 : blue;
		return cc.color(red,green,blue,color.a);
	},
	
	AddBlue : function(color, addvalue){
		var blue = color.b + addvalue;

		var div = 0;
		if (blue > 255){
			div = Math.round((255 - blue) * 0.5);
			blue = 255;
		}
		if (blue < 0){
			div = Math.round((0 - blue) * 0.5);
			blue = 0;
		}

		if (div == 0)
			return cc.color(color.r,color.g,blue,color.a);

		var green = color.g;
		var red = color.r;
		green = green + div;
		green = green<0 ? 0 : green;
		green = green>255? 255 : green;
		red = red + div;
		red = red<0 ? 0 : red;
		red = red>255? 255 : red;
		return cc.color(red,green,blue,color.a);
	},
	
	_divs : [
	         [-1,1,1],[1,-1,1],[1,1,-1],
	         [1,-1,-1],[-1,1,-1],[-1,-1,1],
	         [-1,1,0],[0,-1,1],[-1,0,1],
	         [1,-1,0],[0,1,-1],[1,0,-1],
	         [0,1,1],[1,0,1],[1,1,0],
	         [1,0,0],[0,1,0],[0,0,1],
	         [0,-1,-1],[-1,0,-1],[-1,-1,0],
	         [-1,0,0],[0,-1,0],[0,0,-1] ],

     GetRandomColor : function(color, div, idx){
    	 var multiply = Math.floor(idx / 24) + 1;
    	 idx = idx % 24;
    	 if (this._divs[idx][0] != 0)
    		 color = this.AddRed(color, this._divs[idx][0] * div * multiply);
    	 if (this._divs[idx][1] != 0)
    		 color = this.AddGreen(color, this._divs[idx][1] * div * multiply);
    	 if (this._divs[idx][2] != 0)
    		 color = this.AddBlue(color, this._divs[idx][2] * div * multiply);
    	 return color;
     }
};